﻿
Partial Class WebForm_ManageEmployee_AddEmployee
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Me.Visible = False

        If Not IsPostBack Then

        End If
    End Sub

    'Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

    '    If Me.SaveData() = True Then
    '        RaiseEvent Saved(Me, New EventArgs)
    '    End If

    '    'ElseIf Me.DataAccessMode = My.DataAccessMode.Update Then
    '    '    Me.UpdateData()
    '    '    RaiseEvent Updated(Me, New EventArgs)

    'End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Me.BindDDLActive()
        Me.BindDDLOrganization()

        If Me.fv.CurrentMode = FormViewMode.Insert Then
            'Me.BindDataForInsert()
        End If
    End Sub

    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand
        If e.CommandName = "btnSave" Then
            If Me.SaveData() = True Then
                RaiseEvent Saved(Me, New EventArgs)
            End If
        ElseIf e.CommandName = "btnSelectOrganization" Then
            'Me.SearchOrganizationControl1.Show()
        ElseIf e.CommandName = "Canceled" Then
            RaiseEvent Canceled(Me, New EventArgs)
        End If
    End Sub
End Class

Partial Class WebForm_ManageEmployee_AddEmployee
    Private Sub BindDDLActive()
        Dim ddlActive As DropDownList = TryCast(Me.fv.FindControl("ddlActive"), DropDownList)

        ddlActive.DataTextField = "Text"
        ddlActive.DataValueField = "Value"
        ddlActive.DataSource = MyObj.GetCodeMappingData("Organization", "Active")
        ddlActive.DataBind()
    End Sub

    Private Sub BindDDLOrganization()
        Dim ddlOrganizationID As DropDownList = TryCast(Me.fv.FindControl("ddlOrganizationID"), DropDownList)
        Dim sql As New MyOrganizationDataFilter
        Dim dat As New clsData

        sql.Length = 0
        sql.AppendFormat("select o.*, dbo.GetUOrganizationID(o.OrganizationID,-1) as UOrganizationID ")
        sql.AppendFormat(" from Organization o ")
        sql.AppendFormat("where Active = 'Y' ")

        ddlOrganizationID.DataTextField = "OrganizationName"
        ddlOrganizationID.DataValueField = "OrganizationID"
        ddlOrganizationID.DataSource = dat.GetData(sql)
        ddlOrganizationID.DataBind()

        ddlOrganizationID.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())

        ddlOrganizationID.Text = DataBinder.Eval(fv.DataItem, "OrganizationID")
    End Sub

    ''' <summary>
    ''' 新增模式
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Show()
        Me.DataAccessMode = My.DataAccessMode.Insert
        'Me.OrganizationID = organizationID

        If Me.fv.CurrentMode = FormViewMode.Insert Then
            Me.fv.DataBind()
        Else
            Me.fv.ChangeMode(FormViewMode.Insert)
        End If


        'Me.BindDataForInsert()
        'Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    ''' <summary>
    ''' 編輯模式
    ''' </summary>
    ''' <param name="employeeID"></param>
    ''' <remarks></remarks>
    Public Sub ShowEdit(ByVal employeeID As String)

        Me.fv.ChangeMode(FormViewMode.Edit)

        Me.BindDataForEdit(employeeID)
        'Me.mpSearch.Show()
        Me.Visible = True
    End Sub

    Private Sub BindDataForInsert()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select o.* ")
        sql.AppendFormat("from Organization o ")
        sql.AppendFormat("where o.OrganizationID = {0} ", Me.OrganizationID)

        Dim dt As Data.DataTable = dat.GetData(sql)
        'MyObj.ShowMessage(TryCast(Me.fv.FindControl("txtOrganizationName"), Label).Text)
        TryCast(Me.fv.FindControl("txtOrganizationName"), Label).Text = dt.Rows(0)("OrganizationName").ToString()
    End Sub

    Private Sub BindDataForEdit(ByVal employeeID As String)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select e.*, o.OrganizationName ")
        sql.AppendFormat("from Employee e ")
        sql.AppendFormat("inner join Organization o on e.OrganizationID = o.OrganizationID ")
        sql.AppendFormat("where e.EmployeeID = '{0}' ", employeeID.Replace("'", "''"))

        Dim dt As Data.DataTable = dat.GetData(sql)

        Me.fv.DataSource = dt
        Me.fv.DataBind()

        TryCast(Me.fv.FindControl("txtEmployeeID"), TextBox).Enabled = False
        'TryCast(Me.fv.FindControl("txtWindowsID"), TextBox).Enabled = False
    End Sub

    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            If Me.fv.CurrentMode = FormViewMode.Insert Then
                sql.Length = 0

                If TryCast(Me.fv.FindControl("txtPassword"), TextBox).Text <> "" Then
                    sql.AppendFormat("insert Employee (EmployeeID,EmployeeName,Email,PhoneNumber,FaxPhoneNumber,OrganizationID,Active,Password) select ")
                Else
                    sql.AppendFormat("insert Employee (EmployeeID,EmployeeName,Email,PhoneNumber,FaxPhoneNumber,OrganizationID,Active) select ")
                End If

                sql.AppendFormat(" '{0}' ", TryCast(Me.fv.FindControl("txtEmployeeID"), TextBox).Text.Replace("'", "''"))
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtEmployeeName"), TextBox).Text.Replace("'", "''"))
                'sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtWindowsID"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtEmail"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtPhoneNumber"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtFaxPhoneNumber"), TextBox).Text)
                'sql.AppendFormat(",{0} ", Me.OrganizationID)
                sql.AppendFormat(",{0} ", TryCast(Me.fv.FindControl("ddlOrganizationID"), DropDownList).SelectedValue)
                sql.AppendFormat(",'{0}' ", "Y")

                If TryCast(Me.fv.FindControl("txtPassword"), TextBox).Text <> "" Then
                    sql.AppendFormat(",'{0}' ", clsAuth.EncryptPassword(TryCast(Me.fv.FindControl("txtPassword"), TextBox).Text))
                End If

                Dim builder As New InsertStringBuilder("Employee")

                builder.AddAssignment("EmployeeID", Me.fv.FindTxtControl("txtEmployeeID").Text.Replace("'", "''"))
                builder.AddAssignment("EmployeeName", Me.fv.FindTxtControl("txtEmployeeName").Text.Replace("'", "''"))
                'builder.AddAssignment("WindowsID", Me.fv.FindTxtControl("txtWindowsID").Text)
                builder.AddAssignment("Email", Me.fv.FindTxtControl("txtEmail").Text)
                builder.AddAssignment("PhoneNumber", Me.fv.FindTxtControl("txtPhoneNumber").Text)
                builder.AddAssignment("FaxPhoneNumber", Me.fv.FindTxtControl("txtFaxPhoneNumber").Text)

                builder.AddAssignment("OrganizationID", Me.fv.FindDDLControl("ddlOrganizationID").SelectedValue)

                If TryCast(Me.fv.FindControl("txtPassword"), TextBox).Text <> "" Then
                    builder.AddAssignment("Password", clsAuth.EncryptPassword(Me.fv.FindTxtControl("txtPassword").Text))
                End If

                builder.AddAssignment("Active", "Y")

                'MyObj.WriteLog(builder.ToString())

                dat.AccData(builder)
            ElseIf Me.fv.CurrentMode = FormViewMode.Edit Then
                sql.Length = 0
                sql.AppendFormat("update Employee set ")
                sql.AppendFormat(" EmployeeName='{0}' ", TryCast(Me.fv.FindControl("txtEmployeeName"), TextBox).Text.Replace("'", "''"))
                'sql.AppendFormat(",WindowsID='{0}' ", TryCast(Me.fv.FindControl("txtWindowsID"), TextBox).Text)
                sql.AppendFormat(",Email='{0}' ", TryCast(Me.fv.FindControl("txtEmail"), TextBox).Text)
                sql.AppendFormat(",PhoneNumber='{0}' ", TryCast(Me.fv.FindControl("txtPhoneNumber"), TextBox).Text)
                sql.AppendFormat(",FaxPhoneNumber='{0}' ", TryCast(Me.fv.FindControl("txtFaxPhoneNumber"), TextBox).Text)

                sql.AppendFormat(",OrganizationID='{0}' ", TryCast(Me.fv.FindControl("ddlOrganizationID"), DropDownList).SelectedValue)

                If TryCast(Me.fv.FindControl("txtPassword"), TextBox).Text <> "" Then
                    sql.AppendFormat(", Password = '{0}' ", clsAuth.EncryptPassword(TryCast(Me.fv.FindControl("txtPassword"), TextBox).Text))
                End If

                sql.AppendFormat("where EmployeeID='{0}' ", TryCast(Me.fv.FindControl("txtEmployeeID"), TextBox).Text.Replace("'", "''"))

                dat.AccData(sql)
            End If

            scope.Complete()
        End Using

        Return True
    End Function

    Public ReadOnly Property EmployeeID() As String
        Get
            Return TryCast(Me.fv.FindControl("txtEmployeeID"), TextBox).Text
        End Get
    End Property

    Public ReadOnly Property EmployeeName() As String
        Get
            Return TryCast(Me.fv.FindControl("txtEmployeeName"), TextBox).Text
        End Get
    End Property

    Private Property OrganizationID() As String
        Get
            Return ViewState("OrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("OrganizationID") = value
        End Set
    End Property

    Private Property DataAccessMode() As My.DataAccessMode
        Get
            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As My.DataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property

    Public Event Canceled(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event Saved(ByVal sender As Object, ByVal e As System.EventArgs)
End Class